#include "BlockQueue.hpp"
#include <time.h>

void* Producer(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        bq->push(rand()%10);
        cout << "生产了一个任务" << " pthread_id :"<< pthread_self() << endl;
    }
}

void* Consumer(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        sleep(1);
        int num = bq->pop();
        cout << "消费了一个任务 : " << num << endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    BlockQueue<int>* bq = new BlockQueue<int>();
    pthread_t c[3], p[5];

    for(int i = 0; i < 5; ++i)
    {
        pthread_create(p+i, nullptr, Producer, bq);
    }
    for(int i = 0; i < 3; ++i)
    {
        pthread_create(c+i, nullptr, Consumer, bq);
    }
    for(int i = 0; i < 5; ++i)
    {
        pthread_join(p[i],nullptr);
    }
    for(int i = 0; i < 3; ++i)
    {
        pthread_join(c[i],nullptr);
    }

    delete bq;
    return 0;
}